//
//  MonthTicketViewController.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/12/22.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "MonthTicketViewController.h"
#import "MonthTicketTableCell.h"
#import "MonthTicketModel.h"
#import "MonthTicketViewModel.h"
#import "MainInfoTitleFields.h"
#import "H5BaseViewController.h"
#import "PayTicketViewController.h"

@interface MonthTicketViewController ()

@property (weak, nonatomic) IBOutlet UITableView *mainTable;

- (IBAction)buyBtnAction:(UIButton *)sender;

@property (weak, nonatomic) IBOutlet UILabel *price;
@property (weak, nonatomic) IBOutlet UILabel *dayCount;
@property (assign, nonatomic) NSInteger dayCountNumber;


@property (strong, nonatomic) MonthTicketViewModel *monthTicketViewModel;

@property (assign, nonatomic) NSInteger selectedIndex;


- (IBAction)ruleBtnAction:(UIButton *)sender ;
- (IBAction)replayBtnAction:(UIButton *)sender;

@end

@implementation MonthTicketViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.selectedIndex = -1;
    self.monthTicketViewModel = [[MonthTicketViewModel alloc ] init];
    [self loadData];
    [self setLeftBtnItem];
}

-(void)loadData{
    
    self.monthTicketViewModel.LineId = @(self.entity.LineId).stringValue;
    self.monthTicketViewModel.BCID   = self.entity.BCID;
    self.monthTicketViewModel.StartStationId = self.entity.StartStationId;
    self.monthTicketViewModel.EndStationId = self.entity.EndStationId;
    
    [self.monthTicketViewModel loadMonthTicketData:^(NSInteger resultType, id data, NSString *errorMsg) {
        if (resultType == 1) {
            [self.mainTable reloadData];
        }else{
            [SVProgressHUD showErrorWithStatus:errorMsg];
        }
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.selectedIndex == indexPath.row) {
        self.selectedIndex = -1;
        [self clear];
    }else{
        self.selectedIndex = indexPath.row;
        MonthTicketModel *ticketModel = [self.monthTicketViewModel.monthTicketList objectAtIndex:indexPath.row];
        [self updateBottomData:ticketModel];
    }
    
    [self.mainTable reloadData];
}

-(void)updateBottomData:(MonthTicketModel *)ticketModel{
    
    self.entity.TicketMonth = ticketModel.Month;
    self.price.text = ticketModel.SalePrice.stringValue;
    self.dayCountNumber = ticketModel.WorkDay;
    self.dayCount.attributedText = [self getDayCountString];
    
    
    
}

-(void)clear{
    self.price.text = @"0";
    self.dayCountNumber = 0;
    self.dayCount.attributedText = [self getDayCountString];
}

-(NSAttributedString *)getDayCountString{
    
    NSMutableArray *mutableString = [[NSMutableArray alloc ] init];
    [mutableString addObject:[MainInfoTitleFields getInstaceWithText:@"共" hexColor:GLOBLE_TEXT_COLOR_HEX fontSize:@"13"]];
    [mutableString addObject:[MainInfoTitleFields getInstaceWithText:@(self.dayCountNumber).stringValue hexColor:GLOBLE_ORANGE_COLOR_HEX fontSize:@"17"]];
    [mutableString addObject:[MainInfoTitleFields getInstaceWithText:@"天" hexColor:GLOBLE_TEXT_COLOR_HEX fontSize:@"13"]];
    return [CommoneTools getAttributeString:mutableString];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.monthTicketViewModel.monthTicketList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MonthTicketTableCell *monthCell = [tableView dequeueReusableCellWithIdentifier:@"MonthTicketTableCell"];
    monthCell.indexPath= indexPath;
    [monthCell setModel:[self.monthTicketViewModel.monthTicketList objectAtIndex:indexPath.row] selectedIndex:self.selectedIndex];
    return monthCell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


// 规则
- (IBAction)ruleBtnAction:(UIButton *)sender {
    H5BaseViewController *h5Base = [[H5BaseViewController alloc ] init];
    h5Base.webViewUrl = BUYRETURNTICKET;
    [self.navigationController pushViewController:h5Base animated:YES];
}

// 申请班次
- (IBAction)replayBtnAction:(UIButton *)sender {
    H5BaseViewController *h5Base = [[H5BaseViewController alloc ] init];
    h5Base.webViewUrl = ADDCLASSCOUNT;
    [self.navigationController pushViewController:h5Base animated:YES];
}

- (IBAction)buyBtnAction:(UIButton *)sender {
    
    if (self.selectedIndex == -1) {
        [SVProgressHUD showErrorWithStatus:@"请选择月票"];
        return;
    }
    
    PayTicketViewController *payTicket = [[UIStoryboard storyboardWithName:@"Ticket" bundle:nil]  instantiateViewControllerWithIdentifier:@"PayTicketViewController"];
    payTicket.payTicketStyle = PayTicket_Month;
    payTicket.entity         = self.entity;
    payTicket.totalDay = @(self.dayCountNumber).stringValue;
    payTicket.totalDayPrice = self.price.text;
    
    [self.navigationController pushViewController:payTicket animated:YES];
    
}
@end
